Is there a more efficient way instead of multiple FOR loops and IF statements